package com.blackcat.dynamicexcel.demo.controller;


import com.blackcat.dynamicexcel.demo.service.DynamicExcelAnewService;
import com.blackcat.dynamicexcel.demo.vo.ExcelFrontEndParameter;
import com.blackcat.dynamicexcel.service.DynamicExcelService;
import com.sun.media.sound.SoftTuning;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Excel模板 前端控制器
 * </p>
 *
 * @author blackcat
 * @since 2022-12-23
 */
@RestController
@RequestMapping("/excel")
public class ExcelTemplateController {

    @Autowired
    private DynamicExcelAnewService dynamicExcelService;


    @RequestMapping(value = "/dynamicImport",method = RequestMethod.GET)
    public Integer dynamicImport(MultipartFile file,int excelId,@RequestParam(required = false) String... additionalParam) throws IOException {
        Map<String, Object> additionalParamMap = new HashMap<>();
        if (additionalParam != null && additionalParam.length > 0) {
            for (String str : additionalParam) {
                System.out.println("参数：" + str);
                String[] split = str.split(",");
                additionalParamMap.put(split[0], split[1]);
            }
            System.out.println("额外补充参数：" + additionalParamMap);
        }
        return dynamicExcelService.dynamicImport(file,excelId,additionalParamMap);
    }

    @RequestMapping(value = "/dynamicExport",method = RequestMethod.POST)
    public void dynamicExport(@RequestBody ExcelFrontEndParameter param) throws IOException {
        dynamicExcelService.dynamicExport(param);
    }



}
